<template>
  <div>
    <!-- style会自动挂载到根元素 -->
    <Chart :option="chartOption" style="height: 400px"></Chart>
  </div>
</template>

<script>
import Chart from "@/components/Chart";
import random from "lodash/random";
export default {
  name: "Analysis",
  data() {
    return {
      chartOption: {
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      },
    };
  },
  mounted() {
    // 让数据随机
    this.interval = setInterval(() => {
      this.chartOption.series[0].data = this.chartOption.series[0].data.map(
        () => random(100)
      );

      // 深度监听解决,重新改变对象
      this.chartOption = { ...this.chartOption };
    }, 3000);
  },

  // 必须在组件销毁之前清除掉定时器
  beforeDestroy() {
    clearInterval(this.interval);
    this.interval = null;
  },

  components: {
    Chart,
  },
};
</script>

<style></style>
